package com.yumijinfu.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yumijinfu.R;
import com.yumijinfu.activity.RenChouDetailsActivity;
import com.yumijinfu.entity.Page;

import java.util.List;

/**
 * Created by ekikousei易皇星 on 16/11/16.
 * E-mail:13764664731@163.com
 * Signature:缘分是本书,翻的不经意会错过,读的太认真会流泪!!
 * <p/>
 * TODO:类描述:
 */
public class RenChouAdapter extends BaseAdapter {
    private Context context;
    private List<Page> infos;
    private LayoutInflater inflater;
    //
    private GouMaiOnClickListener mGouMaiOnClickListener;
    public RenChouAdapter(Context context,List<Page> infos){
        this.context=context;
        this.infos=infos;
         inflater = LayoutInflater.from(context);

        //
        mGouMaiOnClickListener=new GouMaiOnClickListener();

    }

    public  void  UpdateAdapter(List<Page> infos){

        this.infos=infos;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return infos.size();
    }

    @Override
    public Object getItem(int position) {
        return infos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        if(viewHolder==null){
            viewHolder=new ViewHolder();
            convertView= inflater.inflate(R.layout.confess_item,null);
            viewHolder.tv_goumai= (TextView) convertView.findViewById(R.id.tv_goumai);
            //
            viewHolder.tv_goumai.setTag(infos.get(position));
            viewHolder.tv_goumai.setOnClickListener(mGouMaiOnClickListener);

            //
            viewHolder.tv_annualInterestRate= (TextView) convertView.findViewById(R.id.tv_annualInterestRate);
            viewHolder.tv_borrowTitle= (TextView) convertView.findViewById(R.id.tv_borrowTitle);
            viewHolder.tv_borrowTimeLimit= (TextView) convertView.findViewById(R.id.tv_borrowTimeLimit);


            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }

        Page info = infos.get(position);
        viewHolder.tv_annualInterestRate.setText(info.getAnnualInterestRate()+"%");
        viewHolder.tv_borrowTitle.setText(info.getBorrowTitle());
        if(info.getIsDay().equals("1")){
            viewHolder.tv_borrowTimeLimit.setText(info.getBorrowTimeLimit()+"天");
        }else {
            viewHolder.tv_borrowTimeLimit.setText(info.getBorrowTimeLimit()+"月");
        }


        return convertView;
    }


    /**
     * 购买事件监听
      */

    class GouMaiOnClickListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            Page info= (Page) v.getTag();
            Intent intent=new Intent(context,RenChouDetailsActivity.class);
            intent.putExtra("info",info);
            context.startActivity(intent);
        }
    }

    public   final class ViewHolder{

        TextView  tv_goumai;  //购买

        TextView tv_annualInterestRate;

        TextView tv_borrowTitle;

        TextView tv_borrowTimeLimit;


    }



}
